
function renderItem(params, api) {
    var categoryIndex = api.value(0);
    var start = api.coord([api.value(1), categoryIndex]);
    var end = api.coord([api.value(2), categoryIndex]);
    var height = api.size([0, 1])[1] * 0.6;

    return {
        type: 'rect',
        shape: echarts.graphic.clipRectByRect({
            x: start[0],
            y: start[1] - height / 2,
            width: end[0] - start[0],
            height: height
        }, {
            x: params.coordSys.x,
            y: params.coordSys.y,
            width: params.coordSys.width,
            height: params.coordSys.height
        }),
        style: api.style()
    };
}

function UnixToDate(unixTime, isFull, timeZone) {
    if (typeof (timeZone) == 'number')
    {
        unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60 * 1000;
    }
    var time = new Date(unixTime);
    var ymdhis = "";
    if (isFull === true)
    {
        if(time.getUTCHours()<10){
            ymdhis += "0"+time.getUTCHours() + ":";
        }else{
            ymdhis += time.getUTCHours() + ":";
        }
        if(time.getUTCMinutes()<10){
            ymdhis += "0" + time.getUTCMinutes();
        }else{
            ymdhis += time.getUTCMinutes();
        }
        
    }
    return ymdhis;
}
// 车位监控图
function initCar(data) {
    var thisData = data.wulianwang[4].tccAllInfo;
    console.log(thisData);
    var cheweiNum = [];
    var dataV = {};
    console.log(thisData['监控信息']);
    if(JSON.stringify(thisData['监控信息']) == "{}"){
            dataV = {
                        "202032": [
                        {
                            "duration": 6279000,
                            "basetime": 1504281600000
                        },
                        {
                            "duration": 5115000,
                            "basetime": 1504288306000
                        },
                        {
                            "duration": 43246000,
                            "basetime": 1504301344000
                        },
                        {
                            "duration": 549000,
                            "basetime": 1504344807000
                        },
                        {
                            "duration": 18000,
                            "basetime": 1504345373000
                        },
                        {
                            "duration": 416000,
                            "basetime": 1504345864000
                        },
                        {
                            "duration": 20546000,
                            "basetime": 1504347454000
                        }
                        ],
                        "202033": [
                        {
                            "duration": 26781000,
                            "basetime": 1504281600000
                        },
                        {
                            "duration": 970000,
                            "basetime": 1504309268000
                        },
                        {
                            "duration": 297000,
                            "basetime": 1504319778000
                        },
                        {
                            "duration": 11141000,
                            "basetime": 1504325564000
                        },
                        {
                            "duration": 385000,
                            "basetime": 1504337244000
                        },
                        {
                            "duration": 6787000,
                            "basetime": 1504337777000
                        },
                        {
                            "duration": 4383000,
                            "basetime": 1504345215000
                        },
                        {
                            "duration": 14000,
                            "basetime": 1504349681000
                        },
                        {
                            "duration": 1461000,
                            "basetime": 1504349766000
                        },
                        {
                            "duration": 16741000,
                            "basetime": 1504351259000
                        }
                        ],
                        "202034": [
                        {
                            "duration": 36120000,
                            "basetime": 1504281600000
                        },
                        {
                            "duration": 4463000,
                            "basetime": 1504318507000
                        },
                        {
                            "duration": 2361000,
                            "basetime": 1504323350000
                        },
                        {
                            "duration": 3120000,
                            "basetime": 1504325757000
                        },
                        {
                            "duration": 11065000,
                            "basetime": 1504329389000
                        },
                        {
                            "duration": 396000,
                            "basetime": 1504341195000
                        },
                        {
                            "duration": 25948000,
                            "basetime": 1504342052000
                        }
                        ],
                        "202035": [
                        {
                            "duration": 1819000,
                            "basetime": 1504316260000
                        },
                        {
                            "duration": 2226000,
                            "basetime": 1504318647000
                        },
                        {
                            "duration": 9286000,
                            "basetime": 1504321497000
                        },
                        {
                            "duration": 3360000,
                            "basetime": 1504330937000
                        },
                        {
                            "duration": 66000,
                            "basetime": 1504334465000
                        },
                        {
                            "duration": 3327000,
                            "basetime": 1504335945000
                        },
                        {
                            "duration": 287000,
                            "basetime": 1504339330000
                        },
                        {
                            "duration": 523000,
                            "basetime": 1504341702000
                        },
                        {
                            "duration": 25438000,
                            "basetime": 1504342562000
                        }
                        ],
                        "202036": [
                        {
                            "duration": 255000,
                            "basetime": 1504281600000
                        },
                        {
                            "duration": 10033000,
                            "basetime": 1504283505000
                        },
                        {
                            "duration": 874000,
                            "basetime": 1504309306000
                        },
                        {
                            "duration": 11000,
                            "basetime": 1504311706000
                        },
                        {
                            "duration": 84000,
                            "basetime": 1504312549000
                        },
                        {
                            "duration": 21000,
                            "basetime": 1504314063000
                        },
                        {
                            "duration": 369000,
                            "basetime": 1504316632000
                        },
                        {
                            "duration": 118000,
                            "basetime": 1504317577000
                        },
                        {
                            "duration": 158000,
                            "basetime": 1504319318000
                        },
                        {
                            "duration": 395000,
                            "basetime": 1504319613000
                        },
                        {
                            "duration": 39000,
                            "basetime": 1504321465000
                        },
                        {
                            "duration": 7538000,
                            "basetime": 1504325422000
                        },
                        {
                            "duration": 5727000,
                            "basetime": 1504333687000
                        },
                        {
                            "duration": 188000,
                            "basetime": 1504340020000
                        },
                        {
                            "duration": 27673000,
                            "basetime": 1504340327000
                        }]
                    };
    }else{
        dataV = thisData['监控信息'];
    }
    for(var key in dataV){
        cheweiNum.push(key);
    }

    var carChart = echarts.init(document.getElementById('chewei-echart'));

    var carData = [];
    var categories = cheweiNum;
    console.log(categories);
    var types = [
        {name: 'JS Heap', color: '#7b9ce1'},
        {name: 'Documents', color: '#bd6d6c'},
        {name: 'Nodes', color: '#75d874'},
        {name: 'Listeners', color: '#e0bc78'},
        {name: 'GPU Memory', color: '#dc77dc'},
        {name: 'GPU', color: '#72b362'}
    ];

    echarts.util.each(categories, function (category, index) {
        for (var i = 0; i < dataV[category].length; i++) {
            var typeItem = types[Math.round(Math.random() * (types.length - 1))];
            var duration = dataV[category][i].duration;
            var baseTime = dataV[category][i].basetime;
            carData.push({
                name: typeItem.name,
                value: [
                    index,
                    baseTime,
                    baseTime += duration,
                    duration
                ],
                itemStyle: {
                    normal: {
                        color: typeItem.color
                    }
                }
            });
        }
    });

    console.log(carData);

    var carOption = {
        title: {
            left: 'center'
        },
        legend: {
            data: ['bar', 'error']
        },
        dataZoom: [{
            type: 'slider',
            filterMode: 'weakFilter',
            showDataShadow: false,
            top: 200,
            height: 10,
            borderColor: 'transparent',
            backgroundColor: '#e2e2e2',
            handleIcon: 'M10.7,11.9H9.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4h1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7v-1.2h6.6z M13.3,22H6.7v-1.2h6.6z M13.3,19.6H6.7v-1.2h6.6z', // jshint ignore:line
            handleSize: 20,
            handleStyle: {
                shadowBlur: 6,
                shadowOffsetX: 1,
                shadowOffsetY: 2,
                shadowColor: '#aaa'
            },
            labelFormatter: ''
        }, {
            type: 'inside',
            filterMode: 'weakFilter'
        }],
        grid: {
            height:150,
            left: '18%',
            top: '5%',
        },
        xAxis: {
            scale: true,
            axisLabel: {
                formatter: function (val) {
                    return UnixToDate(val, true, 8);
                },
                textStyle:{
                    color:'#ccc',
                    fontSize: 12
                }
            },
            axisLine: {
                lineStyle: {
                    color: '#ccc'
                }
            }
        },
        yAxis: {
            data: categories,
            axisLabel: {
                textStyle:{
                    color:'#ccc',
                    fontSize: 12
                }
            },
            axisLine: {
                lineStyle: {
                    color: '#ccc'
                }
            }
        },
        series: [{
            type: 'custom',
            renderItem: renderItem,
            itemStyle: {
                normal: {
                    opacity: 0.8
                }
            },
            encode: {
                x: [1, 2],
                y: 0
            },
            data: carData
        }]
    };
    carChart.setOption(carOption);
}

function initTing(data){
    // 停车场监控图
    var thisData = data.wulianwang[4].tccAllInfo;
    var tccDay=[];
    var tccRail=[];
    var tccUse=[];
    for( var key in thisData.rate){
        tccDay.push(key.slice(5).replace("-",""));
        tccRail.push(thisData.rate[key]['周转率']);
        tccUse.push(thisData.rate[key]['占用率']);
    }
    var tingChart = echarts.init(document.getElementById('tingche-echart'));
    var tingOption = {
        color: ['#b63633','#16c9d3'],
        // 提示框组件
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'none'
            }
        },
        legend: {
            data: ['占用率','周转率'],
            textStyle: {
                color: '#ccc',
                fontSize: 14
            },
            top: 20,
            itemWidth: 40,
            itemHeight: 20,
        },
        xAxis: [
            {
                type: 'category',
                boundaryGap: true,
                data:tccDay,
                axisLabel: {            // 坐标轴刻度标签
                    textStyle:{
                        color:'#ccc',
                        fontSize: 14
                    },
                    interval: 0
                },
                axisLine: {
                lineStyle: {
                    color: '#ccc'
                }
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                interval: 100,
                axisLabel: {
                    formatter: '{value}',
                    textStyle:{
                        color:'#ccc',
                        fontSize: 14
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: '#ccc'
                    }
                },
                splitLine: {
                    "show": false
                }
            }
        ],
        series: [
            {
                name: '占用率',
                type: 'line',
                symbol: 'emptyCircle',   // 标记的图形
                symbolSize:15,
                showAllSymbol: true,
                smooth: true,
                data:tccUse
            },
            {
                name: '周转率',
                type: 'bar',
                barWidth: '30%',
                itemStyle: {
                normal: {
                    barBorderRadius: 10
                }
                },
                barGap: '-100%',
                data: tccRail
            }
        ]
    };
    tingChart.setOption(tingOption);
}

function initAll(data) {
    initTing(data);
    initCar(data);
}

$.ajax({
    type : "get",
    url : baseUrl + "/wlw/indexAll",
    dataType : "json",
    cache : false,
    data:{},
    success : function(data) {
        initAll(data);   
    }
})
